package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Insert extends JInternalFrame {
	private JLabel label;
	private JLabel lblNewLabel;
	private JTextField code;
	private JTextField ea;
	private JButton btnInsert;
	private JLabel status;
	
	Connection conn; 	 //connection 객체 생성.
	DatabaseTest main;


	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Insert frame = new Insert();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Insert(Connection c, DatabaseTest main) { 	 //
		super("입력", true, true, true, false);
		setBounds(0, 0, 300, 350);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnInsert());
		getContentPane().add(getStatus());
		
		this.conn = c;	 //
		this.main = main;
	}
	

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제품코드:");
			label.setBounds(37, 30, 57, 15);
		}
		return label;
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("수량:");
			lblNewLabel.setBounds(37, 56, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(99, 27, 138, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(99, 52, 138, 21);
		}
		return ea;
	}
	private JButton getBtnInsert() {
		if (btnInsert == null) {
			btnInsert = new JButton("저장");
			btnInsert.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent ev) {
					try {
						String c = code.getText();
						int e = Integer.parseInt(ea.getText());
						
						String sql = "insert into ptbl(serial, code, ea) values(ptbl_serial.nextval,?,?)";  //저장버튼 누르면 ptbl테이블에 (?.?)미완성된 값이 저장..
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c); //1번째  ?값에  c값(코드)을 넣어라..
						pstmt.setInt(2,e); //2번째  ?값에  e값(수량)을 넣어라..
						
						pstmt.executeUpdate(); //executeUpdate만 날리면 된다.. executeInsert,executeDelete는없음.
												// 무조건..executeUpdate
							
			
						if(main.select != null) main.select.setList();  //select에 setList()로 호출.
						
												//왜? select에서 열어도, insert에서 열어도 둘다 수정되게 하려고
												//setList로 따로 뺌.
			
			
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					
				}
			});
			btnInsert.setBounds(85, 109, 97, 23);
		}
		return btnInsert;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setHorizontalAlignment(SwingConstants.CENTER);
			status.setBounds(12, 296, 260, 15);
		}
		return status;
	}
}
